package GUI.gameLobbyWin;

import java.awt.Dimension;
import java.awt.Font;
import java.util.LinkedList;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

import protocol.Message;

@SuppressWarnings("serial")
public class ChatMessagesPanel extends JPanel {
	private LinkedList<Message> messages;
	
	public ChatMessagesPanel() {
		super();
		messages = new LinkedList<>();
		setMinimumSize(new Dimension(200, 200));
	}
	
	public void addMessage(Message msg, boolean sync) {
		if(!sync&&msg.msgID>messages.size()+1) {
			GameLobbyWindow.getInstance().requestSyncMessages();
			return;
		}
		for(Message message: messages)
			if(message.msgID==msg.msgID)
				return;
		messages.add(msg);
		JPanel msgPanel = new JPanel();
		msgPanel.setLayout(new BoxLayout(msgPanel, BoxLayout.PAGE_AXIS));
		String msgString;
		if(msg.senderName!=null)
			msgString = "<html><div style=\"width: 395px\"><b>"+msg.senderName+":</b> "+msg.message+"</div></html>";
		else
			msgString = "<html><b>"+msg.message+"</b></html>";
		JLabel msgLabel = new JLabel(msgString);
		msgLabel.setFont(new Font("Lucida Console", Font.PLAIN, 12));
		msgPanel.add(msgLabel);
		add(msgPanel);
		if(!sync) {
			repaint();
			revalidate();
		}
	}
	
	public void syncMessages(LinkedList<Message> msgs) {
		for(Message msg: msgs)
			addMessage(msg, true);
		repaint();
		revalidate();
	}
}
